<template>
	<view class="login">
		<!-- #ifndef MP-ALIPAY -->
			<cu-custom bgColor="bg-gradual-blue" :isBack="true">
				<block slot="backText">返回</block>
				<block slot="content">登录</block>
			</cu-custom>
		<!-- #endif -->
		<view class="login-con">
			<form>
				<view class="cu-form-group">
					<view class="title">AccessToken</view>
					<input placeholder="输入AccessToken" name="input" v-model="accesstoken"></input>
					<text class='cuIcon-scan text-orange' @tap="scanner"></text>
				</view>
				<view class="padding flex flex-direction">
					<button class="cu-btn bg-gradual-green lg" @tap="login">登录</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	import Vue from 'vue'
	import getApp from '../../App.vue'
	export default {
		data() {
			return {
				accesstoken: ''
			};
		},
		methods: {
			scanner() { //扫码获取token
				uni.scanCode({
					success: (res) => {
						if (res.errMsg === 'scanCode:ok') {
							this.accesstoken = res.result
						} else{
							uni.showToast({
								title: res.errMsg,
								icon: 'none'
							})
						}
					},
					fail(err) {
						uni.showToast({
							title: err,
							icon: 'none'
						})
					}
				})
			},
			async login() { //登录
				const res = await this.$api.login(this.accesstoken)
				uni.setStorageSync('token',this.accesstoken)
				uni.setStorageSync('userInfo',JSON.stringify(res))
				getApp.globalData.accesstoken = this.accesstoken
				getApp.globalData.userInfo = res
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">

</style>
